﻿using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Windows.Forms;

namespace PrintUnit
{
    public class PrintHelper
    {
        private List<Bitmap> bitmapList = new List<Bitmap>();

        private int pageNumber;

        private PrintDocument PrintDocuments { get; set; }

        private PrintPreviewDialog printPreviewDialog { get; set; }

        private string Url { get; set; }

        public PrintHelper(string url)
        {
            this.Url = url;
        }

        private void PrintPage(object sender, PrintPageEventArgs e)
        {
            e.HasMorePages = true;

            Bitmap bitmap = bitmapList[pageNumber];
            Image image = (Image)bitmap.Clone();
            e.Graphics.DrawImage(image, 0, 0);
            pageNumber++;
            if (pageNumber == bitmapList.Count())
            {
                e.HasMorePages = false;
            }
            //SetPrintMsg( saleBill);
            //PrintDocument.Print();
        }

        public void PrintPreview<T>(List<T> saletable, dynamic paramArray)
        {
            printPreviewDialog = new PrintPreviewDialog() { };
            SetPrintdocment();
            
            printPreviewDialog.Document = PrintDocuments;
            SetPrintMsg(saletable, paramArray);
            printPreviewDialog.ShowDialog();
        }

        public void Print<T>(List<T> saleTable,dynamic paramArray)
        {
            SetPrintdocment();
            SetPrintMsg(saleTable, paramArray);
            PrintDocuments.Print();
        }

        private void SetPrintdocment()
        {
            PrintDocuments = new PrintDocument();
            //PrintDocuments.PrinterSettings.DefaultPageSettings.Margins.Top = 4;
            //PrintDocuments.PrinterSettings.DefaultPageSettings.Margins.Left = 4;
            //PrintDocuments.PrinterSettings.DefaultPageSettings.Margins.Right = 4;
            //PrintDocuments.PrinterSettings.DefaultPageSettings.Margins.Bottom = 4;
            PrintDocuments.DefaultPageSettings.PaperSize = new PaperSize("22", 300, 500);
            PrintDocuments.PrintPage += new PrintPageEventHandler(this.PrintPage);
        }

        private void SetPrintMsg<T>(List<T> saletable, dynamic paramArray)
        {
            pageNumber = 0;
            bitmapList = new List<Bitmap>();
            bitmapList.Clear();
            XMLmodle lmodle = new XMLmodle();
            //var ddf = new { id = 2, dd = 3 };
            Size size = new Size();
            int left = PrintDocuments.DefaultPageSettings.Margins.Left;
            int top = PrintDocuments.DefaultPageSettings.Margins.Top;
            //size.Width = PrintDocuments.DefaultPageSettings.PaperSize.Width - left - PrintDocuments.DefaultPageSettings.Margins.Right;
            //size.Height = PrintDocuments.DefaultPageSettings.PaperSize.Height - top - PrintDocuments.DefaultPageSettings.Margins.Bottom;
            size.Width = PrintDocuments.DefaultPageSettings.PaperSize.Width-20 ;
            size.Height = PrintDocuments.DefaultPageSettings.PaperSize.Height-20;
            lmodle.SetMsg(Url, saletable, paramArray, size, ref bitmapList);
        }
    }
    
}
